Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/efi: Add HostEnvironment bits for efi/preinstall #314

Conversation

chrisccoulson
Copy link
Collaborator

@chrisccoulson chrisccoulson commented Jul 10, 2024

This extends the internal HostEnvironment interface to accommodate
efi/preinstall, to make it a superset of the one that's already exposed by the
efi package. The intention is that this superset won't be exposed for now,
other than for unit testing - the efi package still only exposes the existing
subset with methods for overriding EFI variables and the TCG log.

The defaultEnvImpl in has been updated to provide a default implementation of
it, and the internal/efitest package has been updated to be able to mock the
new parts of it.

@chrisccoulson chrisccoulson force-pushed the internal-efi-add-preinstall-host-environment branch 3 times, most recently from 0d5a105 to dbf3805 Compare July 10, 2024 14:35
This extends the HostEnvironment interface to accommodate efi/preinstall,
to make it a superset of the one that's already exposed by the efi package.
The intention is that this superset won't be exposed for now, other than
for unit testing - the efi package still only exposes the existing
subset.

The defaultEnvImpl in has been updated to provide a default implementation of
it, and the internal/efitest package has been updated to be able to mock the
new parts of it.
@chrisccoulson chrisccoulson force-pushed the internal-efi-add-preinstall-host-environment branch from dbf3805 to 1bc68cf Compare July 11, 2024 20:31
@chrisccoulson chrisccoulson requested a review from pedronis July 11, 2024 20:33
@chrisccoulson chrisccoulson marked this pull request as ready for review July 11, 2024 21:05
Copy link
Collaborator

@pedronis pedronis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, did a pass

HasCPUIDFeature(feature uint64) bool

// ReadMSRs reads the value of the specified MSR for all CPUs,
// returning a map of the result for all CPU numbers
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe "a map of the results keyed by the CPU numbers" would be clearer here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated this.

Comment on lines +139 to +145
return nil, ErrNoDeviceAttribute
}

f, err := os.Open(filepath.Join(d.path, attr))
switch {
case os.IsNotExist(err):
return nil, ErrNoDeviceAttribute
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these error path are no tested

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a test for this

return nil, err
}
if !fi.Mode().IsRegular() {
return nil, ErrNoDeviceAttribute
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for this

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a test for this

switch {
case os.IsNotExist(err):
// it's ok to have no devices for the specified class
return nil, nil
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this case is not tested

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a test for this

efi/shim_test.go Outdated
@@ -400,7 +400,7 @@ func (s *shimSuite) TestShimSbatPolicyLatestUnset(c *C) {

c.Assert(visitor.varModifiers, HasLen, 1)

collector := NewVariableSetCollector(efitest.NewMockHostEnvironment(nil, nil))
collector := NewVariableSetCollector(efitest.NewMockHostEnvironment(efitest.MockVars{}, nil))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the reason for the need of this change and other similar ones?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it was because I made a change to MockHostEnvironment.VarContext to not attach a efi.VarsBackend if the supplied efitest.MockVars map is nil, which resulted in anything that ends up calling efi.ReadVariable crashing. I reverted the change to how it worked before - MockHostEnvironment.VarContext will always add a efi.VarsBackend even if the supplied efitest.MockVars is nil. This does mean that anything which calls efi.ReadVariable ends up reading keys from a nil map, but that seems to be ok in go (it's only adding entries to a nil map that causes a panic)

The previously committed change only added an implementation of
efi.VarsBackend to the supplied context if the supplied
efitest.MockVars was not nil, but this resulted in existing tests having
to explicitly create an empty efitest.MockVars.

Revert to the existing behaviour where we add an implementation of
efi.VarsBackend to the supplied context in all cases. Although the
implementation is a nil map, that is ok for reading (it's only adding
keys to a nil map that causes go to panic).
@chrisccoulson chrisccoulson requested a review from pedronis July 23, 2024 11:12
Copy link
Collaborator

@pedronis pedronis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you

@chrisccoulson chrisccoulson merged commit 945a2da into canonical:master Jul 23, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants